<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .right {
            border: 1px solid red;
        }

        .cuo {
            border: 1px solid greenyellow;
        }
    </style>
</head>

<body>
    <form action="">
        <input type="text" name="name"><span></span>
    </form>
    <script>
        const reg = /^[a-zA-Z0-9-_]{6,16}$/;    //制定规则
        const texs = document.querySelector('input');
        const span = document.querySelector('span');
        console.log(texs);
        texs.addEventListener('blur', function () {
            // console.log(111);
            // console.log(texs.value);
            console.log(reg.test(texs.value));
            if (reg.test(texs.value)) {
                span.innerHTML = '符合规范';
                if (document.querySelector('.cuo')) {
                    document.querySelector('.cuo').classList.remove('cuo');
                }
                //span.className = 'right'; 直接覆盖类名
                span.classList.add('right')
            } else {
                span.innerHTML = '不符合规范';
                span.classList.add('cuo');
                //span.className = 'cuo'; 直接覆盖类名

            }
        })
    </script>
</body>

</html>